package com.stockz.ui.swing.charting.painter.shape;

import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.util.List;

import com.stockz.core.model.Bar;
import com.stockz.ui.swing.charting.model.Line;
import com.stockz.ui.swing.charting.painter.axis.VolumeAxisPainter;
import com.stockz.ui.swing.charting.util.MathUtil;
import com.stockz.ui.swing.charting.util.SwingUtil;
import com.stockz.ui.swing.charting.util.UIChartingConstant;
import com.stockz.ui.swing.charting.view.StockChartPanel;
import com.stockz.ui.swing.charting.view.StockChartPanelSkin;
import com.stockz.ui.swing.charting.view.StockChartPanelState;

public class LinePainter{

	public static void paint(Graphics2D g, Line shape, Rectangle r) {
		StockChartPanel panel = shape.getStockChartPanel();
		if(panel == null) return;
		StockChartPanelState state = panel.getStockChartPanelState();
		StockChartPanelSkin skin = panel.getStockChartPanelSkin();
		
		Point start = SwingUtil.fitPoint(state, shape.getStart());
		Point end = SwingUtil.fitPoint(state, shape.getEnd());
		r = state.getDrawingRectangle();
		g.setClip(r);
		if(start.x == Integer.MAX_VALUE 
				|| start.x == Integer.MIN_VALUE
				|| start.y == Integer.MAX_VALUE
				|| start.y == Integer.MIN_VALUE){
			SwingUtil.restrictPointOutside(start, r);		
			paintValues(panel,shape,g,start);
		}
		
		if(end.x == Integer.MAX_VALUE 
				|| end.x == Integer.MIN_VALUE
				|| end.y == Integer.MAX_VALUE
				|| end.y == Integer.MIN_VALUE){
			SwingUtil.restrictPointOutside(end, r);			
			paintValues(panel,shape,g,end);
		}
		
		g.setColor(skin.getDrawingColor());
		g.drawLine(start.x, start.y, end.x, end.y);
		g.setClip(null);
	}
	
	public static void paintValues(StockChartPanel panel, Line shape, Graphics2D g, Point point){
		StockChartPanelState state = panel.getStockChartPanelState();
		StockChartPanelSkin skin = panel.getStockChartPanelSkin();
		Rectangle rectangle = state.getDrawingRectangle();
		
		if(point.x < rectangle.x){
			String text = UIChartingConstant.priceNumberFormat.format(shape.getStart().getPrice());
			Rectangle r = g.getFont().getStringBounds(text, g.getFontRenderContext()).getBounds();
			SwingUtil.drawInfoRect(point.x - r.width-3, point.y,text , g, skin);
		}
		if(point.x > rectangle.x+rectangle.width){
			SwingUtil.drawInfoRect(point.x, point.y, VolumeAxisPainter.formatVolume(
					state.getMaximumVolumeVisible(), 
					MathUtil.unfitY(state.getVolumeRectangle().y, state.getVolumeRectangle().y + state.getVolumeRectangle().height, state.getMinimumVolumeVisible(), state.getMaximumVolumeVisible(), 
							MathUtil.fitY(state.getPriceRectangle().y, state.getPriceRectangle().y + state.getPriceRectangle().height, state.getMinimumPriceVisible(), state.getMaximumPriceVisible(), shape.getStart().getPrice(), state.isSemiLog()), state.isSemiLog())), g, skin);
		}
		if(point.y > rectangle.y + rectangle.height){
			int index = shape.getStart().getIndex();
			List<Bar> barList = state.getBarList();
			if(index >= 0 && index < barList.size()){
				String text = UIChartingConstant.dateFormat.format(barList.get(index).getDate());
				Rectangle r = g.getFont().getStringBounds(text, g.getFontRenderContext()).getBounds();
				SwingUtil.drawInfoRect(point.x - r.width/2, point.y + 3,text, g, skin);
			}
		}
	}
}
